(function(angular) {
	'use strict';
	angular.module('fs.waf').controller('cda.payCode.deleteController', deleteController);
	deleteController.$inject = [ '$scope', 'Constants', 'modalService', 'layoutConfig', 'validatorConfig', 'gridService', 'authService', 'payCodeService' ];
	function deleteController($scope, Constants, modalService, layoutConfig, validatorConfig, gridService, authService, payCodeService) {
		var ctrl = this, payCodeInfo = $scope.$stateParams.params;
		ctrl.layoutConfig = layoutConfig;
		ctrl.validatorConfig = validatorConfig;

		ctrl.init = function() {
			ctrl.payCode = {};
			payCodeService.initDropDownDataOptions($scope, ctrl).then(function() {
				authService.delete(payCodeInfo).then(function(response) {
					ctrl.payCode = response.data || payCodeInfo;
					payCodeService.initGeneral($scope, ctrl);
				});
			});
		};
		
		ctrl.deleteSubmit = function() {
			//search if there some data in profAccount
			ctrl.payCode.profSetlInstList = ctrl.payCode.profSetlInstList ||[];
			if(ctrl.payCode.profSetlInstList.length >0){
				modalService.openErrorDialogs({
					'messages' : ["js.global.error.foreignkey-conflict"]
				});
			}else {
				modalService.openConfirmDialogs({
					'messages' : [ {msg : 'title.dialog.confirm.delete', args : [ctrl.payCode.primaryKey]} ],
					'btnCallback' : {
						'ok' : function(response) {
							authService.deleteSubmit(ctrl.payCode).then(deleteSuccessful, deleteFailed);
						}
					}
				});
			}
		};
		
		function deleteSuccessful(response) {
			modalService.openAcknowledgeDialogs({
				'messages' : [ 'title.dialog.successful.delete' ],
				'btnCallback' : {
					'close' : function() {
						$scope.go('cda.payCode.list');
					}
				},
				'redirections' : [ {
					'name' : 'title.infobar.workflow.list',
					'state' : 'cda.workflow.wfSetup.list',
					'params' : {}
				}, {
					'name' : 'title.infobar.paycode.list',
					'state' : 'cda.payCode.list',
					'params' : {}
				}]
			});
		}

		function deleteFailed(response) {
			modalService.openErrorDialogs({
				'messages' : response.error
			});
		}
	}
})(angular);